$(document).ready(function() {
	$("input[value='actor']").change(function() {
		if ($(this).is (':checked')){			
			$.get("./jsp/user/actor-properties.jsp", function( my_var ) {
				$("#actor-properties").html(my_var);
			});
		}
		else{
			$("#actor-properties").empty();
		}	       
	});
	$("#detailed-features").click(function(){
		if($("#detailed-features-output").is(":visible"))
			{
			$("#detailed-features-output").hide();
			}
		else
			{
			$("#detailed-features-output").show();
			}
	});
	$("#contact").click(function(){
		if($("#contact-output").is(":visible"))
			{
			$("#contact-output").hide();
			}
		else
			{
			$("#contact-output").show();
			}
	});
	 $('#profile-options').change(function() {
						var val = $("#profile-options option:selected").val();
						if (val == 1)
							window.location.href = "user?who=me";
						else if (val == 2)
							window.location.href = "edit-profile";
						else if (val == 3)
							window.location.href = "change-password";
						else
							window.location.href = "user?who=me";
					});
	 
	 $('#project-options').change(function() {
		 var val = $("#project-options option:selected").val();
		 var mid= $('#movie-id').val();
         if(val==1)
        	 window.location.href="movie?which="+mid;
         else if(val==2)
        	 window.location.href="edit-movie?which="+mid;
         else
        	 window.location.href="movie?which="+mid;
	 });
	 
	 var countA=$("input[name='numofactors']").attr("value");
	 var countC=$("input[name='numofcrews']").attr("value");
	 $("#addtitle").click(function(){
		 var val = $("#titles option:selected").val();
		 var txt = $("#titles option:selected").text();
         if(val=="actor"){
        	 countA++;
        	 $.get("./jsp/user/project-actor-properties.jsp", function( my_var ) {
        		my_var= my_var.replace( new RegExp("AA1", 'g'), "AA"+countA );
        		$("input[name='numofactors']").attr("value",countA);
  				$("#wanted").append(my_var);
  				$("#cancelAA"+countA).click(function(){
					 var divid=$(this).attr("alt");
					 $("#div"+divid).remove();
				 });
  			});
         }
         else {
        	 countC++;
        	 $.get("./jsp/user/project-crew-properties.jsp", function( my_var ) {
        		 my_var= my_var.replace( new RegExp("CC1", 'g'), "CC"+countC );
        		 $("input[name='numofcrews']").attr("value",countC);
 				$("#wanted").append(my_var);
 				$("input[name='titleCC"+countC+"']").attr("value",val);
 				$("input[name='titleShowCC"+countC+"']").attr("value",txt);
 				$("#cancelCC"+countC).click(function(){
 					 var divid=$(this).attr("alt");
 					 $("#div"+divid).remove();
 				 });
 			}); 		
        }
	 });
});

function cancelJob(aid)
{
	var div = document.getElementById("div" + aid);
	div.parentNode.removeChild(div);
}

function registerFormValidation()
{
	var firstname = document.register.firstname;
	var surname = document.register.surname;
	var email = document.register.email;
	var password = document.register.password;
	var password2 = document.register.password2;
	var gender = document.register.gender;
	var city = document.register.city;
	var tel = document.register.tel;
	var title = document.register.title;
	var height = document.register.height;
	var weight = document.register.weight;
	
	if(firstname.value=="" || firstname.value==null)
	{
		firstname.focus();
		alert("Firstname must be entered!");
	}
	else if(surname.value==null || surname.value=="")
	{
		alert("Surname must be entered!");
	}
	else if(email.value==null || email.value=="")
	{
		alert("Email must be entered!");
	}
	else if(!isValidEmail(email))
	{
		alert("Email not valid!");
	}
	else if(password.value==null || password.value=="")
	{
		alert("Password must be entered!");
	}
	else if(!isValidPassword(password))
	{
		alert("Password must be between a length of 8 and 16!");
	}
	else if(password2.value != password.value)
	{
		alert("Passwords must match!");
	}
	else if(tel.value != "" && !allnumeric(tel))
	{
		alert("Tel no must have only digits!");
		
	}
	else if(height !=null && weight !=null)
	{
		if(!allnumeric(height) || !allnumeric(weight))
		{
			alert("Height and weight must be integer");
		}
		
		else
		{
			document.register.submit();
			//return true;
		}
	}	
	else
	{
		document.register.submit();
		//return true;
	}
}

function editProfileValidation()
{
	var firstname = document.editProfile.firstname;
	var surname = document.editProfile.surname;
	var city = document.editProfile.city;
	var tel = document.editProfile.tel;
	var height = document.editProfile.height;
	var weight = document.editProfile.weight;
	
	if(firstname.value=="" || firstname.value==null)
	{
		firstname.focus();
		alert("Firstname must be entered!");
	}
	else if(surname.value==null || surname.value=="")
	{
		alert("Surname must be entered!");
	}
	else if(tel.value != "" && !allnumeric(tel))
	{
		alert("Tel no must have only digits!");
		
	}
	else if(height !=null && weight !=null)
	{
		if(!allnumeric(height) || !allnumeric(weight))
		{
			alert("Height and weight must be integer");
		}
		
		else
		{
			document.editProfile.submit();
			//return true;
		}
	}	
	else
	{
		document.editProfile.submit();
		//return true;
	}
}

function commentValidation()
{
	var comment = document.commentOn.comment;
	
	if(comment.value==null || isEmpty(comment))
	{
		alert("Comment cannot be empty!");
	}
	else
	{
		var url=$("input[name='unique']").attr('value');
		$("input[name='currurl']").attr("value",url);
		document.commentOn.submit();
		//return true;
	}
}

function ratingValidation()
{
		var url=$("input[name='unique']").attr('value');
		$("input[name='currurl']").attr("value",url);
		document.rating.submit();
}


function allnumeric(uzip)  
	{   
	var numbers = /^[0-9]+$/;  
	if(uzip.value.match(numbers))  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  
function isValidEmail(email)  
{  
	var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
	if(email.value.match(mailformat))  
	{  
		return true;  
	}  
	else  
	{		  
		return false;  
	}  
}
function isEmpty(input)  
{  
	var mailformat = /^\s+$/;  
	if(input.value.match(mailformat))  
	{  
		return true;  
	}  
	else  
	{		  
		return false;  
	}  
}

function isValidPassword(password)  
{  
	var length= password.value.length;
	if(length >7 && length < 17)  
	{  
		return true;  
	}  
	else  
	{		  
		return false;  
	}  
}

function allLetter(uname)  
{   
	var letters = /^[A-Za-z]+$/;  
	if(uname.value.match(letters))  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  